package com.shimao.celnet.payment.service.impl;

import com.github.pagehelper.PageHelper;
import com.shimao.celnet.payment.form.BGoodsCodeForm;
import com.shimao.celnet.payment.mapper.BGoodsCodeMapper;
import com.shimao.celnet.payment.model.BGoodsCode;
import com.shimao.celnet.payment.service.BCtaxGoodsCodeService;
import com.shimao.celnet.payment.service.BGoodsCodeService;
import com.shimao.celnet.payment.service.SCodeNameService;
import com.shimao.celnet.payment.util.FinalUtil;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;

/**
 * 开票商品编码管理
 * Created by ensure on 2017/12/3.
 */
@Service
public class BGoodsCodeServiceImpl extends BaseServiceImpl<BGoodsCode> implements BGoodsCodeService {
    protected Logger logger = Logger.getLogger(this.getClass());


    @Autowired//国税商品编码设定
    private BCtaxGoodsCodeService bCtaxGoodsCodeService;

    @Autowired
    private BGoodsCodeMapper bGoodsCodeMapper;

    @Autowired//code名称，取得 自定义商品最大编码
    private SCodeNameService sCodeNameService;
    /**
     * 分页查询
     * @param formBean
     * @param pageNum
     * @param length
     * @return
     */
    @Override
    public BGoodsCodeForm getByPage(BGoodsCodeForm formBean, int pageNum, int length) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(BGoodsCode.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(formBean.getCtaxGoodsCode())) {
            //国税商品编码
            criteria.andLike("ctaxGoodsCode", "%" + formBean.getCtaxGoodsCode()+ "%");
        }

        if (StringUtil.isNotEmpty(formBean.getGoodsCode())) {
            //商品编码
            criteria.andLike("goodsCode", "%" + formBean.getGoodsCode()+ "%");
        }

        if (StringUtil.isNotEmpty(formBean.getGoodsName())) {
            //商品名称
            criteria.andLike("goodsName", "%" + formBean.getGoodsName()+ "%");
        }


        //分页查询
        PageHelper.startPage(pageNum, length);
        List<BGoodsCode> tableList = getByExample(example);
        formBean.setPageInfo(tableList);
        return  formBean;
    }

    /**
     *新增保存
     * @return
     */
    @Override
    public int save(BGoodsCode sBean){
        int retCnt=0;
        String loginUserGuid = FinalUtil.INTER_FACE_USER_ID;
        sBean.setUpdateUserGuid(loginUserGuid);
        sBean.setCreatedUserGuid(loginUserGuid);
        sBean.setGuid(getGuid());//guid
        retCnt = super.save(sBean);

        return retCnt;
    }

    /**
     *修改保存
     * @return
     */
    @Override
    public int update(BGoodsCode sBean){
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        int retCnt = super.update(sBean);
        return retCnt;
    }

    /**
     *存在更新，不存在新增保存
     * @return 自定义商品编码
     */
    @Override
    public String saveUpdate(BGoodsCode sBean){

        //查询;
        Example example = new Example(BGoodsCode.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(sBean.getGoodsName())) {
            //自定义商品名称
            criteria.andEqualTo("goodsName",  sBean.getGoodsName() );
        }

        List<BGoodsCode> tableList = getByExample(example);
        if(tableList.size()>0 && tableList.size()==1){
            //存在：更新
            sBean=tableList.get(0);
             this.update(sBean);
        }else{
            //不存在：新增
            String loginUserGuid = FinalUtil.INTER_FACE_USER_ID;
            sBean.setUpdateUserGuid(loginUserGuid);
            sBean.setCreatedUserGuid(loginUserGuid);
            sBean.setGoodsCode(sCodeNameService.getInvoiceSellerNumber());//自定义商品编码设定
            this.save(sBean);
        }
        return sBean.getGoodsCode();
    }
    /**
     * 查出自定义商品编码，根据商品名称
     */
    public BGoodsCodeForm getGoodsInfo(String goodsName){
      List<BGoodsCodeForm>  bGoodsCodeList= bGoodsCodeMapper.selectGoodsInfo(goodsName);
      if (bGoodsCodeList.size()>0)
      {
          return bGoodsCodeList.get(0);
      }
      return null;
    }
}
